package com.idroid.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.R;

/**
 * Content:优化后的Toast
 * 可以设置背景颜色
 * @version 1.0.0
 */

public class MToast {

    public static void shortToast(Context context, int resId) {
        showToast(context, context.getString(resId), Toast.LENGTH_SHORT);
    }

    public static void shortToast(Context context, String text) {
        showToast(context, text, Toast.LENGTH_SHORT);
    }

    public static void shortToast(Context context, String text, int gravity) {
        showToast(context, text, Toast.LENGTH_SHORT, gravity);
    }

    public static void shortToast(Context context, String text, int gravity,int background) {
        showToast(context, text, Toast.LENGTH_SHORT, gravity,background);
    }

    public static void longToast(Context context, int resId) {
        showToast(context, context.getString(resId), Toast.LENGTH_LONG);
    }

    public static void longToast(Context context, String text) {
        showToast(context, text, Toast.LENGTH_LONG);
    }

    public static void shortToast(Context context, int resId, int gravity) {
        showToast(context, context.getString(resId), Toast.LENGTH_SHORT, gravity);
    }

    public static void longToast(Context context, int resId, int gravity) {
        showToast(context, context.getString(resId), Toast.LENGTH_LONG, gravity);
    }

    public static void longToast(Context context, String text, int gravity) {
        showToast(context, text, Toast.LENGTH_LONG, gravity);
    }

    public static void longToast(Context context, String text, int gravity,int background) {
        showToast(context, text, Toast.LENGTH_LONG, gravity,background);
    }

    public static void showToast(Context context, String text, int duration) {
        showToast(context,text,duration, Gravity.BOTTOM);
    }

    public static void showToast(Context context, String text, int duration, int gravity) {
        showToast(context,text,duration, gravity,0);
    }

    public static void customerToast(Context context,String text,int textColor,int background,int gravity){
        if (context != null && !TextUtils.isEmpty(text)) {
            Toast toast = new Toast(context);
            LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflate.inflate(R.layout.layout_toast, null);
            LinearLayout llParent = (LinearLayout) v.findViewById(R.id.ll_parent);
            if(background!=0)
                llParent.setBackgroundResource(background);
            TextView tv = (TextView) v.findViewById(R.id.toast_message);
            if(textColor!=0)
                tv.setTextColor(context.getResources().getColor(textColor));
            tv.setText(text);
            if(gravity != Gravity.BOTTOM)
                toast.setGravity(gravity, 0, 0);
            toast.setView(v);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    public static void showToast(Context context, String text, int duration, int gravity,int background) {
        if (context != null && !TextUtils.isEmpty(text)) {
            Toast toast = new Toast(context);
            LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflate.inflate(R.layout.layout_toast, null);
            LinearLayout llParent = (LinearLayout) v.findViewById(R.id.ll_parent);
            if(background!=0)
                llParent.setBackgroundResource(background);
            TextView tv = (TextView) v.findViewById(R.id.toast_message);
            tv.setText(text);
            if(gravity != Gravity.BOTTOM)
                toast.setGravity(gravity, 0, 0);
            toast.setView(v);
            toast.setDuration(duration);
            toast.show();
        }
    }
}
